﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace QuickRunCore.Helper
{
    /// <summary>
    /// 单击事件委托
    /// </summary>
    public delegate void OnClickEventHandler(object sender, EventArgs e);

    public partial class ButtonItem : UserControl
    {

        private System.Windows.Forms.ToolTip ToolTip;

        /// <summary>
        /// 单击事件
        /// </summary>
        public new event OnClickEventHandler OnClick;

        public new event EventHandler OnMouseEnter;
        #region 属性

        [Category("外观")]
        [Description("内容标题")]
        [DefaultValue("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return lblItemName.Text;
            }
            set
            {
                ToolTip.SetToolTip(this, value);
                ToolTip.SetToolTip(this.pnlMain, value);
                ToolTip.SetToolTip(this.picItemImage, value);
                ToolTip.SetToolTip(this.lblItemName, value);
                lblItemName.Text = value;
            }
        }

        [Category("外观")]
        [Description("文字颜色")]
        [DefaultValue(typeof(Color), "#505050")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Color TextColor
        {
            get
            {
                return lblItemName.ForeColor;
            }
            set
            {
                lblItemName.ForeColor = value;
            }
        }

        [Category("外观")]
        [Description("图片")]
        [DefaultValue("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Image Image
        {
            get
            {
                return picItemImage.Image;
            }
            set
            {
                picItemImage.Image = value;
            }
        }

        #endregion

        public ButtonItem()
        {
            InitializeComponent();

            //增加浮动提示框
            ToolTip = new ToolTip();
            ToolTip.ShowAlways = true;
            ToolTip.AutoPopDelay = 10000;
            ToolTip.InitialDelay = 500;
            ToolTip.ReshowDelay = 500;
            ToolTip.ShowAlways = true;
        }

        private void ButtonItem_MouseEnter(object sender, EventArgs e)
        {
            this.BorderStyle = BorderStyle.FixedSingle;
            pnlMain.BackColor = Color.FromArgb(204, 232, 255);
            if (OnMouseEnter != null)
            {
                OnMouseEnter(this, e);
            }
        }

        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern IntPtr WindowFromPoint(Point p);

        private void ButtonItem_MouseLeave(object sender, EventArgs e)
        {
            Point p = Cursor.Position;
            IntPtr h = WindowFromPoint(p);
            if (pnlMain.Handle != h && picItemImage.Handle != h && lblItemName.Handle != h)
            {
                this.BorderStyle = BorderStyle.None;
                pnlMain.BackColor = Color.FromArgb(250, 250, 250);
            }
        }

        private void ButtonItem_OnClick(object sender, EventArgs e)
        {
            if (OnClick != null)
            {
                OnClick(this, e);
            }
        }
    }
}
